<template>
  <div class="app-container">
    <div class="filter-container">
      <el-breadcrumb separator="/" class="breadcrumb-container">
        <el-breadcrumb-item :to="{ path: '/forum/posts' }">帖子列表</el-breadcrumb-item>
        <el-breadcrumb-item>评论管理</el-breadcrumb-item>
      </el-breadcrumb>
      
      <div style="margin: 20px 0;">
        <h3>帖子标题: {{ postTitle }}</h3>
      </div>
    </div>

    <el-table
      v-loading="listLoading"
      :data="list"
      border
      fit
      highlight-current-row
      style="width: 100%;"
    >
      <el-table-column align="center" label="ID" width="80">
        <template slot-scope="scope">
          <span>{{ scope.row.id }}</span>
        </template>
      </el-table-column>

      <el-table-column width="120px" align="center" label="评论用户">
        <template slot-scope="scope">
          <span>{{ scope.row.nickname }}</span>
        </template>
      </el-table-column>

      <el-table-column min-width="300px" label="评论内容">
        <template slot-scope="scope">
          <span>{{ scope.row.content }}</span>
        </template>
      </el-table-column>

      <el-table-column width="180px" align="center" label="评论时间">
        <template slot-scope="scope">
          <span>{{ new Date(scope.row.createTime).getTime() | parseTime('{y}-{m}-{d} {h}:{i}') }}</span>
        </template>
      </el-table-column>

      <el-table-column align="center" label="操作" width="120">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="danger"
            @click="handleDelete(scope.row)"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <div v-if="list.length === 0 && !listLoading" class="empty-text">
      暂无评论
    </div>
  </div>
</template>

<script>
import { getComments, deleteComment } from '@/api/comment'
import { getPostDetail } from '@/api/post'
import { parseTime } from '@/utils'

export default {
  name: 'CommentList',
  data() {
    return {
      list: [],
      listLoading: true,
      postId: null,
      postTitle: ''
    }
  },
  created() {
    this.postId = this.$route.params.postId
    this.getPostDetail()
    this.getList()
  },
  methods: {
    async getList() {
      this.listLoading = true
      try {
        const response = await getComments(this.postId)
        this.list = response.data
      } catch (error) {
        console.error('获取评论列表失败:', error)
        this.list = []
      } finally {
        this.listLoading = false
      }
    },
    async getPostDetail() {
      try {
        const response = await getPostDetail(this.postId)
        this.postTitle = response.data.title
      } catch (error) {
        console.error('获取帖子详情失败:', error)
        this.postTitle = '未知帖子'
      }
    },
    handleDelete(row) {
      this.$confirm('确定要删除这条评论吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        try {
          await deleteComment(this.postId, row.id)
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
          this.getList()
        } catch (error) {
          console.error('删除评论失败:', error)
        }
      })
    }
  }
}
</script>

<style scoped>
.breadcrumb-container {
  margin-bottom: 20px;
}

.empty-text {
  text-align: center;
  padding: 40px;
  color: #909399;
  font-size: 14px;
}
</style>